package com.wanxi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ProductType;
import com.wanxi.service.ProductService;
import com.wanxi.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
@CrossOrigin(origins = "http://localhost:8081")
public class ProductController {
    @Autowired
    ProductService productService;

    @PutMapping("/add")
    @PreAuthorize("hasAuthority('insertProduct')")
    public ResultDTO addProduct(ProductVO productVO,Long productTypeID){
        productVO.setProductType(ProductType.builder().id(productTypeID).build());
        return productService.addProduct(productVO);
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('deleteProduct')")
    public ResultDTO deleteProduct(ProductVO productVO){
        return productService.deleteProduct(productVO);
    }

    @GetMapping("/findAll")
    public ResultDTO findAllProduct(){
        return productService.findAllProduct();
    }

    @PostMapping("/findById")
    public ResultDTO findProductById(ProductVO productVO){
        return productService.findProductById(productVO);
    }

    @GetMapping("/findBySome")
    public ResultDTO findProductBySome(ProductVO productVO){
        return productService.findProductBySome(productVO);
    }

    @PostMapping("/updateEnable")
    @PreAuthorize("hasAuthority('updateProduct')")
    public ResultDTO updateProductEnable(ProductVO productVO){
        return productService.updateEnable(productVO);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('updateProduct')")
    public ResultDTO updateProduct(ProductVO productVO, Long productTypeID){
        productVO.setProductType(ProductType.builder().id(productTypeID).build());
        return productService.updateProduct(productVO);
    }
}
